package org.mule.robo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

public class Table
{

    private Stack[] stacks;

    public Table(int size)
    {
        stacks = new Stack[size];
        for (int i = 0; i < size; i++)
        {
            stacks[i] = new Stack(i);
        }
    }

    public int size()
    {
        return stacks.length;
    }

    public int position(int a)
    {
        return stacks[a].position;
    }

    public Deque<Integer> stack(int a)
    {
        return stacks[a].stack;
    }

    public void updatePosition(int a, int pos)
    {
        stacks[a].position = pos;
    }

    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder("Table:\n-----\n");
        for (int i = 0; i < stacks.length; i++)
        {
            builder.append(i + ": ");
            List reverseList = new ArrayList(stacks[i].stack);
            Collections.reverse(reverseList);
            builder.append(reverseList);
            builder.append("\n");
        }
        builder.append("--- end ---\n");
        return builder.toString();
    }

    public void run(List<Command> commands)
    {
        for (Command command : commands)
        {
            command.execute(this);
        }
        System.out.println("\nfinal layout:\n" + this.toString());
    }
}
